'use strict';
const db = uniCloud.database()
const moment = require('moment')
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event.body)

	if (!event.body) return {
		code: -1,
		message: '参数为空',
		data: null
	}

	const body = event.body
	const userid = event.userid
	const name = event.name
	const unique = event.body.name
	try {
		for (const b of event.body) {
			b.obj.time = String(b.obj.time).replace("年", '-').replace("月", '-').replace("日", ' ')
			const sqlParm = {
				unique,
				userid,
				username: name,
				...b.obj,
				createtime: new Date().toUTCString(),
				date: moment().format('yyyy-MM-dd HH:mm:ss')
			}
			await db.collection("orderRecord").add(sqlParm)
		}
		return {
			code: 0,
			message: '添加成功',
			data: null
		}
	} catch (e) {
		console.log(e);
		return {
			code: -1,
			message: '添加数据库错误',
			data: null
		}
	}
};